package com.guoxiang.bigdata.util;


import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.InputStream;

/**
 * 下载页面的工具类
 */
public class HttpUtil {
    /**
     * 下载页面
     * @param url 下载的页面地址
     * @return 返回html页面的字符串
      */
    public static String simpledDownload(String url){
        //创建http客户端对象
        HttpClient httpClient= HttpClients.createDefault();
        //以get方式请求
        HttpGet httpGet=new HttpGet(url);
        //反爬虫的手段
        //反爬虫的手段
        //httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
//        HttpHost httpHost = new HttpHost("121.61.3.162",9999);
//        RequestConfig requestConf = RequestConfig.custom().setProxy(httpHost).build();
//        httpGet.setConfig(requestConf);

        try {
            //获取请求实体
            HttpResponse response=httpClient.execute(httpGet);
            String html = EntityUtils.toString(response.getEntity());
            //System.out.println(html);
            return html;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 下载完成返回流对象
     *
     * @param url
     * @return
     */
    public static InputStream simpleDownLoadToStream(String url) {
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        //反爬虫的手段
//        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
//        HttpHost httpHost = new HttpHost("125.123.141.75",9999);
//        RequestConfig requestConf = RequestConfig.custom().setProxy(httpHost).build();
//        httpGet.setConfig(requestConf);

        try {
            HttpResponse response = httpClient.execute(httpGet);
            return response.getEntity().getContent();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
